package csel.view.status;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JPanel;
import csel.model.*;
import csel.model.entity.*;
import csel.model.entity.Character;

public class IDPanel extends JPanel implements Observer<CharacterStats>
{	
	private static final long serialVersionUID = 1L;
	
	private BufferedImage myImage;
	private BufferedImage gatorOne;
	private Graphics myGraphics;
	private CharacterStatsHolder myStats;
	
	private String myName;
	private String myOccupation;
	
	static public IDPanel initialize(Character myCharacter)
	{
		IDPanel myIDPanel = new IDPanel(myCharacter);
		myIDPanel.myStats.addObserver(myIDPanel);
		return myIDPanel;
	}
	
	private IDPanel(Character myCharacter)
	{
		myStats = myCharacter.getCharacterStatsHolder();
		myName = myCharacter.getName();
		myOccupation = myCharacter.getOccupation().toString();
		myImage = null;
		try
		{
			gatorOne = ImageIO.read(new File("images/aView.png"));
			myImage = gatorOne;
		}
		catch (IOException e)
		{
			System.out.println("YARRR!  THAR BE IOEXCEPTIONS ON THE STARBOARD BOW!");
		}
		setPreferredSize(new Dimension(myImage.getWidth(),myImage.getHeight()));
		
		myGraphics = myImage.getGraphics();
		myGraphics.setColor(new Color(255, 69, 0));
		myGraphics.fillArc(89,35,40,40,0,360);
		myGraphics.setColor(Color.BLACK);
		myGraphics.drawString(myName, 5, 50);
		myGraphics.drawString(myOccupation, 5, 70);
		myGraphics.setColor(Color.BLUE);
		myGraphics.fillRect(5, 90, 100, 25);
	}

	public void paintComponent(Graphics g)
	{
		super.paintComponent(g);
		g.drawImage(myImage,0,0,null);
	}
	
	public void redrawPanel()
	{
		try
		{
			gatorOne = ImageIO.read(new File("images/aView.png"));
			myImage = gatorOne;
		}
		catch (IOException e)
		{
			System.out.println("YARRR!  THAR BE IOEXCEPTIONS ON THE STARBOARD BOW!");
		}
		myGraphics = myImage.getGraphics();
		myGraphics.setColor(new Color(255, 69, 0));
		myGraphics.fillArc(89,35,40,40,0,(360*myStats.getStat(CharacterStats.CurrentLife)/myStats.getStat(CharacterStats.MaxLife)));
		//myGraphics.fillArc(89,35,(40*myStats.getStat(CharacterStats.CurrentLife)/100),(40*myStats.getStat(CharacterStats.CurrentLife)/100),0,360);
		myGraphics.setColor(Color.BLACK);
		myGraphics.drawString(myName, 5, 50);
		myGraphics.drawString(myOccupation.toString(), 5, 70);
		myGraphics.setColor(Color.BLUE);
		myGraphics.fillRect(5, 90, 100*myStats.getStat(CharacterStats.CurrentMana)/myStats.getStat(CharacterStats.MaxMana), 25);
		repaint();
	}
	
	
	public void setCharacter(Character myCharacter)
	{
		myName = myCharacter.getName();
		myOccupation = myCharacter.getOccupation().toString();
		myStats = myCharacter.getCharacterStatsHolder();
		myStats.addObserver(this);
		redrawPanel();
	}

	public void mupdate(Object o, CharacterStats cs)
	{
		if ((cs == CharacterStats.CurrentLife)||(cs == CharacterStats.CurrentMana))
		{
			redrawPanel();
		}
	}
	
	private void writeObject(java.io.ObjectOutputStream out) throws java.io.IOException
    {
            throw new java.io.NotSerializableException("IDPanel should not be serialized.");
    }
    
    private void readObject(java.io.ObjectInputStream in) throws java.io.IOException
    {
            throw new java.io.NotSerializableException("IDPanel should not be serialized.");
    }

}
